'use strict';
const Service = require('egg').Service;
class LectureRService extends Service {
  async getRelation(lid, uid) {
    const res = await this.app.mysql.get('userlecture_R', {
      lecture_id: lid,
      user_id: uid,
    });
    if (res) {
      const { learn, buy, progress } = res;
      return { learn, buy, progress };
    };
    // 不存在该记录
    return {
      learn: 0,
      buy: 0,
      progress: 0,
    };
  }
  async ownTheLecture(items, uid) {
    items.map(async val => {
      const res = await this.service.mysql.updateOrCreateExist('userlecture_R', {
        buy: 1,
        progress: 0,
        learn: 1,
      }, {
        user_id: uid,
        lecture_id: val.lectureId,
      });
      console.log(res);
    });

  }
  async getAllRelation(uid) {
    const res = await this.app.mysql.query('SELECT * FROM `userlecture_R` JOIN `class` ON `lecture_id` = `class_id` WHERE `user_id` = ? and `ava` = ?', [ uid, 1 ]);
    const cutid = res.map(val => {
      return {
        lecture_id: val.lecture_id,
        buy: val.buy,
        learn: val.learn,
        progress: val.progress,
        classname: val.classname,
        brief: val.brief,
        price: val.price,
        startpic: val.startpic,
      };
    });
    return cutid;
  }
}
module.exports = LectureRService;
